import {
  AddMessage,
  delCartAPI,
  findNewCartListAPI,
  insertCartAPI,
  mergeCartAPI,
  updataAll,
  updateCartItemAPI,
} from "@/apis/cart";
import { defineStore } from "pinia";
import { computed, ref } from "vue";
import { useUserStore } from "./userStore";
export const useCartStore = defineStore(
  "cart",
  () => {
    const userStore = useUserStore();
    const isLogin = computed(() => userStore.userInfo.token);
    const cartList = ref([]);
    const addCart = async (goods) => {
      if (isLogin.value) {
        await insertCartAPI(goods);
        await updateLoginCartList();
      } else {
        const findItem = cartList.value.find(
          (item) => goods.skuId === item.skuId
        );
        if (findItem) {
          findItem.count += goods.count;
        } else {
          cartList.value.push(goods);
        }
      }
    };
    const deCart = async (skuId) => {
      if (isLogin.value) {
        console.log();
        await delCartAPI([skuId]);
        await updateLoginCartList();
      }
      const index = cartList.value.findIndex((item) => item.skuId === skuId);
      cartList.value.splice(index, 1);
    };
    const allCount = computed(() =>
      cartList.value.reduce((a, cartList) => a + cartList.count, 0)
    );
    const allPrice = computed(() =>
      cartList.value.reduce(
        (a, cartList) => a + cartList.count * cartList.price,
        0
      )
    );
    const ids = [];
    //全选功能
    const checkAll = (selected) => {
      cartList.value.forEach((item) => {
        item.selected = selected;
        ids.push(item.skuId);
        const data = {
          selected: selected,
          id: ids,
        };
        updataAllStore(data);
      });
    };
    // 是否全选计算属性
    const isAll = computed(() => {
      return cartList.value.every((item) => item.selected);
    });
    const selectedCount = computed(() =>
      cartList.value
        .filter((item) => item.selected === true)
        .reduce((a, cartList) => a + cartList.count, 0)
    );
    const selectedPrice = computed(() =>
      cartList.value
        .filter((item) => item.selected == true)
        .reduce((a, c) => a + c.count * c.price, 0)
    );
    const updateLoginCartList = async () => {
      const res = await findNewCartListAPI();
      cartList.value = res.result;
    };
    const clearCart = () => {
      cartList.value = [];
    };
    const updateCartItem = async (goods) => {
      const { skuId, count, selected } = goods;
      if (isLogin.value) {
        await updateCartItemAPI(skuId, { count, selected });
      }
    };
    const updataAllStore = async (data) => {
      await updataAll(data);
    };
    const Addshouhuoxinxi = async (data) => {
      await AddMessage(data);
    };
    return {
      cartList,
      allCount,
      allPrice,
      isAll,
      selectedCount,
      selectedPrice,
      updateLoginCartList,
      addCart,
      deCart,
      checkAll,
      clearCart,
      updateCartItem,
      Addshouhuoxinxi,
    };
  },
  {
    persist: true,
  }
);
